# -*- coding: utf-8 -*-
from datetime import date, datetime
from dao.db import db

class PointsRecord(db.Model):
    """积分记录数据模型, 对应points_records表"""
    __tablename__ = 'points_records'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    student_id = db.Column(db.BigInteger, db.ForeignKey('students.id'), nullable=False)
    teacher_id = db.Column(db.BigInteger, db.ForeignKey('teachers.id'), nullable=False)
    points_change = db.Column(db.Integer, nullable=False)
    reason = db.Column(db.String(255), nullable=True)
    created_at = db.Column(db.DateTime, default=datetime.now, nullable=False)
    
    # 关系定义
    student = db.relationship('Student', backref=db.backref('points_records', lazy=True))
    teacher = db.relationship('Teacher', backref=db.backref('points_records', lazy=True))

class PointsReason(db.Model):
    """积分操作理由数据模型，对应points_reasons表"""
    __tablename__ = 'points_reasons'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    reason = db.Column(db.String(255), nullable=False)
    
    __table_args__ = (
        db.Index('idx_points_reason', 'reason'),
        {'comment': '积分操作预设理由表'}
    )
    
    def __repr__(self):
        return f"<PointsReason(id={self.id}, reason='{self.reason}')>"
        
    def to_dict(self):
        """转换为字典，用于接口返回"""
        return {
            "id": self.id,
            "reason": self.reason
        }